package org.leumasjaffe.math.probability;

import java.util.Random;

import org.eclipse.jdt.annotation.NonNull;

import flanagan.analysis.Stat;


public class HyperGeometricRV extends DiscreteRV {
	/** Number of draws */
	int n;
	
	/** Number of successful options */
	int m;
	
	/** Total number of options */
	int N;

	public HyperGeometricRV(int n, int m, int N) {
		this.n = n;
		this.m = m;
		this.N = N;
	}

	public HyperGeometricRV(@NonNull Random r, int n, int m, int N) {
		super(r);
		this.n = n;
		this.m = m;
		this.N = N;
	}

	@Override
	public double getEV() {
		return this.n*this.m/(double)this.N;
	}

	@Override
	public double getVar() {
		return getEV()*((this.N-this.m)/(double)this.N)*(this.N-this.n)/(double)(this.N-1);
	}

	@Override
	public double getP(int k) {
		return Stat.binomialCoeff(this.m, k)*Stat.binomialCoeff(this.N-this.m, this.n-k)/
				(double)Stat.binomialCoeff(this.N, this.n);
	}

	@Override
	public double calcX(double p) {
		if (!isProb(p)) throw new IllegalArgumentException("Arguement is not a valid probability");
		int k = 0;
		while ((p -= getP(k)) > 0) k++;
		return k;
	}

	@Override
	public double getF(int x) {
		double ttl = 0;
		for (int i = 0; i < x; i++)
			ttl += getP(i);
		return ttl;
	}
	
}
